/* 
Project: 		The Impact of Short-Term Employment for Low-Income Youth: Experimental 	
				Evidence from the Philippines
Authors: 		Emily A. Beam and Stella Quimbo

***********************************
Path: 			code/02_analysis
File name: 		table_01.do 
***********************************

Purpose: 		Generates balance table 

Inputs: 		spes_data.dta
Outputs: 		table_01.xlsx							*balance table 
				spes_tables.xlsx/table01_RI_balance		*RI p-values 

Log: 			results/03_output/log_table_01.log		*Contains joint tests

Last updated: 	18 November 2021
*/ 

loc filename "beamquimbo_tables"


cap log close
log using "$output/log_table_01.log",replace

** Reps for RI
loc reps = 1000

#delimit ; 


use "$usedata_analysis/spes_data",clear ;

* Identify covariates for balance tests ;

global balance "_bb_female  _bb_age _bb_edulvl _bb_workany _bb_workformal _bb_worki _bb_la _bb_expected_earn _bb_expt _bb_othexp";


keep _bb_female _bb_edulvl _bb_age  _bb_workany _bb_workformal _bb_worki _bb_la _bb_expected_earn _bb_expt _bb_othexp _f_bb_female _f_bb_edulvl _f_bb_age  _f_bb_workany _f_bb_workformal _f_bb_worki _f_bb_la _f_bb_expected_earn _f_bb_expt 		_f_bb_othexp treatment scell2 endline;

tempfile tempdata ;
save `tempdata', replace;


// fix flags;


foreach var of varlist $balance{;
	
	recode `var' 0 = . if _f`var' == 1;
};


iebaltab  $balance,grpvar(treatment)  total fixedeffect(scell) vce(robust) save("$tables_analysis/table_01") replace;



* RI for individual tests ;

use `tempdata',clear;

local col " "ttest p-value" "RI p-value" ";
local row ""Female" "Age mean" "College" "Any past work experience" "Formal work experience" "Informal work experience" "Lowest acceptable daily wage" "Expected daily wage after grad." "Expected tuition next year" "Expected other educ "";

matrix P = J(10,2,.);
matrix rowname P = `row';
matrix colname P = `col';


local i = 1;

foreach var in $balance{;

areg `var' treatment if _f`var' == 0 , absorb(scel) robust;
testparm treatment;
	matrix P[`i',1]=`r(p)';

	ritest treatment _b[treatment], reps(`reps') seed(1230) strata(scell): areg `var' treatment if _f`var' == 0, a(scell) robust;
		matrix P[`i',2]=r(p);

	local ++i; 
	
};


putexcel set "$tables_analysis/`filename'.xlsx", sheet(table01_RI_balance) modify;
putexcel A1=matrix (P), names nformat(##.###) ;


#delimit ;
* Joint test ;

use `tempdata',clear;

areg treatment $balance _f*,absorb(scel) robust;

testparm $balance;

testparm $balance _f*;

#delimit cr

** SUEST
gen _est2 = 1

ds _bb* _f_bb_edulvl _f_bb_female scell treatment _est* endline,not
loc flags "`r(varlist)'"

di "`flags'"
foreach var of varlist $balance `flags' {

		reg `var'  treatment ,absorb(scell)
		*reg `var'  mflag__methn mflag__hsg  gcard50 wave2 wave4 i.tract_id i.enumerator i.dow i.my
		est store `var'
		}
		
suest $balance `flags'   ,vce(robust)


testparm treatment


log close
